//
//  MessageViewController.swift
//  CloudIMTest
//
//  Created by dengwei on 16/1/11.
//  Copyright © 2016年 dengwei. All rights reserved.
//

import UIKit

extension UIView{
    
   @IBInspectable var cornerRadius:CGFloat {
        get{
            return layer.cornerRadius
        }
        set{
        
            layer.cornerRadius = newValue
            layer.masksToBounds = (newValue > 0)
        }
    }
}

class MessageViewController: UIViewController {
    
    var (userOk,passOk,emailOk) = (false,false,false)
    var possibleInputs : Inputs = []
    

    @IBOutlet weak var usernameText: UITextBox!
    
    @IBOutlet weak var passText: UITextBox!
    
    @IBOutlet weak var emailText: UITextBox!
    
    @IBOutlet weak var regButton: UIButton!
    @IBAction func ShowMenu(sender: UIBarButtonItem) {
       //KxMenu
        var  frame = sender.valueForKey("view")?.frame
        frame?.origin.y = (frame?.origin.y)! + 30
        
        KxMenu.showMenuInView(self.view, fromRect: frame!, menuItems: [
            KxMenuItem("客服1",image: UIImage(named: "share_1.png"),target: self,action:"ClickMenu1"),
            KxMenuItem("客服2",image: UIImage(named: "search_1.png"),target: self,action:"ClickMenu2"),
            KxMenuItem("客服3",image: UIImage(named: "map_2-1.png"),target: self,action:"ClickMenu3"),
            KxMenuItem("客服4",image: UIImage(named: "settingh.png"),target: self,action:"ClickMenu4")
         ])
        //PopMenu
        /*let items = [
            MenuItem(title:"微信",iconName: "share_pyq",glowColor: UIColor.blackColor(),index: 0),
            MenuItem(title:"QQ",iconName: "share_wb",glowColor: UIColor.blueColor(),index: 1),
            MenuItem(title:"微博",iconName: "share_wx",glowColor: UIColor.yellowColor(),index: 2),
            MenuItem(title:"微信",iconName: "share_pyq",glowColor: UIColor.yellowColor(),index: 3),
            MenuItem(title:"QQ",iconName: "share_wb",glowColor: UIColor.yellowColor(),index: 4),
            MenuItem(title:"微博",iconName: "share_wx",glowColor: UIColor.yellowColor(),index: 5)
        ]
        let menu = PopMenu(frame: self.view.bounds,items: items)
        menu.menuAnimationType = .Sina
        if menu.isShowed {
            return
        }
        
        menu.didSelectedItemCompletion = {  (selectedItem:MenuItem!) -> Void in
            
            print(selectedItem.index)
        }
        
        menu.showMenuAtView(self.view)*/
    }
    
    func ClickMenu1(){
        print("菜单1")
    }
    
    func ClickMenu2(){
        print("菜单2")
    }
    func ClickMenu3(){
        print("菜单3")
    }
    func ClickMenu4(){
        print("菜单4")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.regButton.enabled = false
        let tap = UITapGestureRecognizer(target:self,action:"keyboardhidden:")
        self.view.userInteractionEnabled = true
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
        let v1 = AJWValidator(type: .String)
        v1.addValidationToEnsurePresenceWithInvalidMessage("用户名不为空")
        v1.addValidationToEnsureMinimumLength(3, invalidMessage: "用户名至少3位")
        v1.addValidationToEnsureMaximumLength(15, invalidMessage: "最大15位")
        self.usernameText.ajw_attachValidator(v1)
        
        v1.validatorStateChangedHandler = {(newState: AJWValidatorState) -> Void in
            switch newState{
                
                
            case AJWValidatorState.ValidationStateValid:
                //self.userOk = true
                self.possibleInputs.unionInPlace(Inputs.user)
                self.usernameText.highlightState = .Default
                
            default:
                let errorMsg2 = v1.errorMessages.first as? String
                self.possibleInputs.subtractInPlace(Inputs.user)
                //self.userOk = false
                self.usernameText.highlightState = UITextBoxHighlightState.Warning(errorMsg2!)
                
            }
            self.regButton.enabled = self.possibleInputs.isAllOK()
            //self.regButton.enabled = self.userOk && self.passOk && self.emailOk
            //print("userOk\(self.userOk)")
        }
        
        let v2 = AJWValidator(type: .String)
        v2.addValidationToEnsurePresenceWithInvalidMessage("密码不为空")
        v2.addValidationToEnsureMinimumLength(6, invalidMessage: "密码至少6位")
        v2.addValidationToEnsureMaximumLength(15, invalidMessage: "最大15位")
        self.passText.ajw_attachValidator(v2)
        
        v2.validatorStateChangedHandler = {(newState: AJWValidatorState) -> Void in
            switch newState{
                
                
            case AJWValidatorState.ValidationStateValid:
                //self.passOk = true
                self.possibleInputs.unionInPlace(Inputs.pass)
                self.passText.highlightState = .Default
            default:
                let errorMsg2 = v2.errorMessages.first as? String
                //self.passOk = false
                self.possibleInputs.subtractInPlace(Inputs.pass)
                self.passText.highlightState = UITextBoxHighlightState.Warning(errorMsg2!)
                
            }
            //print("passOk\(self.passOk)")
             self.regButton.enabled = self.possibleInputs.isAllOK()
            // self.regButton.enabled = self.userOk && self.passOk && self.emailOk
        }
        
        let v3 = AJWValidator(type: .String)
        v3.addValidationToEnsurePresenceWithInvalidMessage("邮箱不为空")
        v3.addValidationToEnsureValidEmailWithInvalidMessage("邮箱格式不对")
        self.emailText.ajw_attachValidator(v3)
        
        v3.validatorStateChangedHandler = {(newState: AJWValidatorState) -> Void in
            switch newState{
        
            case AJWValidatorState.ValidationStateValid:
               // self.emailOk = true
                self.possibleInputs.unionInPlace(Inputs.mail)
                self.emailText.highlightState = .Default
            default:
                let errorMsg3 = v3.errorMessages.first as? String
               // self.emailOk = false
                self.possibleInputs.subtractInPlace(Inputs.mail)
                self.emailText.highlightState = UITextBoxHighlightState.Warning(errorMsg3!)
                
            }
           // print("emailOk\(self.emailOk)")
             self.regButton.enabled = self.possibleInputs.isAllOK()
            // self.regButton.enabled = self.userOk && self.passOk && self.emailOk
        }
        // Do any additional setup after loading the view.
    }

    func textFieldShouldReturn(textField:UITextField) -> Bool
    {
        //收起键盘
        textField.resignFirstResponder()
        return true
    }
    
    func keyboardhidden(recognizer:UITapGestureRecognizer){
        //隐藏键盘
        self.view.endEditing(true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
